from django.db import models
from rest_framework import serializers
from demo1 import models
import re
# Create your models here.
def checknumbers(val):
    if re.match(r'KJB.*',val):
        return val
    raise serializers.ValidationError('编号不对')



class Teacher(serializers.ModelSerializer):
    class Meta:
        fields='__all__'



        model=models.Teacher


class Myclass(serializers.ModelSerializer):
    tname=serializers.SerializerMethodField(read_only=True)
    def get_tname(self,obj):
        return obj.teacher.name
    class Meta:
        fields='__all__'
        model=models.Myclass
        extra_kwargs={
            'numbers':{'validators':[checknumbers]}
        }







